package com.erlinyou.im.web.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.session.Session;
import org.springframework.web.socket.WebSocketSession;

import com.erlinyou.travel.model.user.TUser;


public class SiteUtil {
	public static final String AUTHENTICATION_KEY="AUTHENTICATION_KEY";
	public static final String SOURCE="SOURCE";
	
	public static Source getSource(Session session){
		int source = session.getAttribute(SOURCE);	
		return Source.valueOf(source);
	}
	
	public static TUser getCurrUser(WebSocketSession session){
		TUser user = (TUser) session.getAttributes().get(AUTHENTICATION_KEY);	
		return user;
	}
	public static TUser getCurrUser(HttpSession httpSession){
		TUser user = (TUser) httpSession.getAttribute(AUTHENTICATION_KEY);	
		return user;
	}
	public static TUser getCurrUser(HttpServletRequest request){
		TUser user = getCurrUser(request.getSession())	;
		return user;
	}
	
	public static Long getCurrUserId(HttpServletRequest request){
		return getCurrUser(request).getUserid();
	}
	
	public static boolean isLogin(HttpSession httpSession){
		return getCurrUser(httpSession) != null;
	}
	public static boolean isLogin(HttpServletRequest request){
		return getCurrUser(request) != null;
	}
}
